package com.demo.activiti;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

/**
 * 流程实例测试类 
 */
public class TestsActiviti {
	private ProcessEngine processEngine = ProcessEngines
			.getDefaultProcessEngine();

	/**
	 * 流程部署 采用加载bpmn方式
	 */
	public Deployment deploy() {
		// 获得repository服务对象
		RepositoryService repositoryService = processEngine
				.getRepositoryService();
		// 获得流程部署对象
		return repositoryService.createDeployment().name("hello")
				.addClasspathResource("diagram/MyProcess.bpmn").deploy();
	}

	/**
	 * 流程部署 采用加载将资源文件压缩的方式
	 * @param path
	 * @param name
	 * @return
	 */
	public Deployment deployByZip(String path, String name) {
		RepositoryService repositoryService = processEngine.getRepositoryService();
		InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
		ZipInputStream zipInputStream = new ZipInputStream(in);
		return repositoryService.createDeployment().addZipInputStream(zipInputStream).name(name).deploy();
	}

	/**
	 * 启动流程实例
	 * 
	 * @return
	 */
	public ProcessInstance flowStart() {
		// 获得流程服务类
		RuntimeService runtimeService = processEngine.getRuntimeService();
		// 获得实例对象
		ProcessInstance instance = runtimeService
				.startProcessInstanceByKey("myProcess");
		return instance;
	}

	/**
	 * 根据名称查询任务列表
	 * 
	 * @param taskExecuteName
	 * @return
	 */
	public List<Task> getTaskList(String taskExecuteName) {
		// 获得任务实例
		TaskService taskService = processEngine.getTaskService();
		// 根据执行者的名称查询任务列表
		List<Task> taskList = taskService.createTaskQuery()
				.taskAssignee(taskExecuteName).list();
		return taskList.size() > 0 ? taskList : null;
	}

	/**
	 * 根据任务ID完成待办任务
	 * 
	 * @param taskId
	 */
	public void completeTask(String taskId) {
		// 获得任务实例
		TaskService taskService = processEngine.getTaskService();
		// 查询是否存在此ID
		long count = taskService.createTaskQuery().taskId(taskId).count();
		if (count == 0) {
			System.out.println("taskID 不存在");
			return;
		} else {
			taskService.complete(taskId);
		}
	}
	/**
	 * 查询当前的任务ID
	 * @return
	 */
	public String getTaskID(){
		// 获得任务实例
		TaskService taskService = processEngine.getTaskService();
		List<Task> tasks = taskService.createTaskQuery().orderByTaskId().desc().list();
		if(tasks == null || tasks.size() == 0)
			return "没有可执行的任务";
		else
			return tasks.get(0).getId();
	}

	public static void main(String[] args) {
		//流程部署
//		System.out.println(new TestsActiviti().deploy());
		//启动流程实例
//		System.out.println(new TestsActiviti().flowStart());
		//查询待办任务
//		System.out.println(new TestsActiviti().getTaskList("请假"));
		//完成流程任务
//		new TestsActiviti().completeTask(new TestsActiviti().getTaskID());
	}

}
